package com.mlethe.library.recyclerview.diff;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.library.recyclerview.adapter.UniversalAdapter;

/**
 * ListUpdateCallback that dispatches update events to the given adapter.
 *
 * @see androidx.recyclerview.widget.DiffUtil.DiffResult#dispatchUpdatesTo(RecyclerView.Adapter)
 *
 * @author Mlethe
 * @date 2020/10/14
 */
public final class AdapterListUpdateCallback implements ListUpdateCallback {

    @NonNull
    private UniversalAdapter mAdapter;

    public AdapterListUpdateCallback(@NonNull UniversalAdapter adapter) {
        this.mAdapter = adapter;
    }

    @Override
    public void onInserted(int position, int count) {
        this.mAdapter.notifyItemRangeInserted(position + mAdapter.getHeaderCount(), count);
    }

    @Override
    public void onRemoved(int position, int count) {
        this.mAdapter.notifyItemRangeRemoved(position + mAdapter.getHeaderCount(), count);
    }

    @Override
    public void onMoved(int fromPosition, int toPosition) {
        this.mAdapter.notifyItemMoved(fromPosition + mAdapter.getHeaderCount(), toPosition + mAdapter.getHeaderCount());
    }

    @Override
    public void onChanged(int position, int count, @Nullable Object payload) {
        this.mAdapter.notifyItemRangeChanged(position + mAdapter.getHeaderCount(), count, payload);
    }
}
